package com.huayushu.controller;

import com.huayushu.enums.VideoStatusEnum;
import com.huayushu.pojo.Bgm;
import com.huayushu.pojo.Users;
import com.huayushu.service.VideoService;
import com.huayushu.utils.HuyushuJSONResult;
import com.huayushu.utils.PagedResult;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author: huayushu luming
 * @date: 2020-01-17 23:32
 * @desc: 测试控制类
 **/
@Controller
@RequestMapping("video")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Value("{FILE_SPACE}")
    private String FILE_SPACE;

    @GetMapping("/showAddBgm")
    public String showAddBgm(){
        return "video/addBgm";
    }

    @GetMapping("/showReportList")
    public String showReportList() {
        return "video/reportList";
    }

    @PostMapping("/reportList")
    @ResponseBody
    public PagedResult reportList(Integer page) {

        PagedResult result = videoService.queryReportList(page, 10);
        return result;
    }

    @PostMapping("/forbidVideo")
    @ResponseBody
    public HuyushuJSONResult forbidVideo(String videoId) {

        videoService.updateVideoStatus(videoId, VideoStatusEnum.FORBID.value);
        return HuyushuJSONResult.ok();
    }

    @GetMapping("/showBgmList")
    public String showBgmList(){
        return "video/bgmList";
    }
    @PostMapping("/delBgm")
    @ResponseBody
    public HuyushuJSONResult delBgm(String bgmId){
         videoService.deleteBgm(bgmId);
         return  HuyushuJSONResult.ok();
    }
    @PostMapping("/queryBgmList")
    @ResponseBody
    public PagedResult queryBgmList(Integer page){
        return videoService.queryBgmList(page,10);
    }

    @PostMapping("/addBgm")
    @ResponseBody
    public HuyushuJSONResult addBgm(Bgm bgm){
        videoService.addBgm(bgm);
        return  HuyushuJSONResult.ok();
    }
    @PostMapping("/bgmUpload")
    @ResponseBody
    public HuyushuJSONResult bgmUpload( @RequestParam("file") MultipartFile[] files) throws Exception {

        //文件保存的命名空间
        //String fileSpace = "E:"+File.separator+"huayushu_video_dev"+File.separator+"mvc-bgm";
        //String fileSpace = "E:"+File.separator+"huayushu_video_dev"+File.separator+"mvc-bgm";
        //保存到数据库中的相对路径
        String uploadPathDB = File.separator+"bgm";

        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        try {
            if (files != null && files.length > 0) {
                String fileName = files[0].getOriginalFilename();
                if (StringUtils.isNotBlank(fileName)) {
                    //文件上传的最终保存路径
                    String finalPath = FILE_SPACE + uploadPathDB + File.separator + fileName;
                    //设置数据库保存的路径
                    uploadPathDB += ("/" + fileName);

                    File outFile = new File(finalPath);
                    if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
                        //创建父文件夹
                        outFile.getParentFile().mkdirs();
                    }
                    fileOutputStream=new FileOutputStream(outFile);
                    inputStream=files[0].getInputStream();
                    IOUtils.copy(inputStream,fileOutputStream);
                }
            }else {
                return HuyushuJSONResult.errorMsg("上传出错了！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return HuyushuJSONResult.errorMsg("上传出错了！");
        }finally {
            if (fileOutputStream != null) {
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        }
//        Users users=new Users();
//        users.setId(userId);
//        users.setFaceImage(uploadPathDB);
//        userService.updateUserInfo(users);

        return HuyushuJSONResult.ok(uploadPathDB);
    }

}
